﻿Public Class frmDemandeDevis

    Public pRubrique As String
    Public pNoDossier As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing
    Dim xOldNom As String
    Dim xOldCabinet As Integer
    Dim xSousRub As String
    Dim xNolig As Long

    Private Sub frmDemandeDevis_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated

        TE_NOM.Focus()

    End Sub

    Private Sub frmDemandeDevis_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = pRubrique & " : " & pNoDossier.ToString
        Me.BackColor = getSocieteColor()
        If pRubrique = "DEM_DEVIS" Then
            ToolStripRubrique.Text = "DEMANDE de DEVIS"
            xSousRub = "DEM_LIG"
        Else
            ToolStripRubrique.Text = "RENDEZ-VOUS"
            xSousRub = "RDVOUS_LIG"
        End If
        InitComboCabinets(CB_CABINETS)
        InitComboQualite(CB_QUALITE)
        InitComboTVA(CB_TVA)
        InitComboAgenda(CB_AGENDA)
        DossierToScreen()
        TabControl1.SelectedIndex = 0

    End Sub

    Private Sub BO_CREER_CLIENT_Click(sender As System.Object, e As System.EventArgs) Handles BO_CREER_CLIENT.Click

        ' Création d'un client
        frmClientMAJ.pCd_cli = 0
        frmClientMAJ.ShowDialog()

    End Sub

    Private Sub BO_WEB_Click(sender As System.Object, e As System.EventArgs) Handles BO_WEB.Click

        If Len(TE_WEBSITE.Text) > 0 Then
            ' Ouvrirr la page web du client
            Process.Start(TE_WEBSITE.Text)
        End If

    End Sub

    Private Sub BO_CLIENT_Click(sender As System.Object, e As System.EventArgs) Handles BO_CLIENT.Click

        ' consultation du client
        frmClientMAJ.pCd_cli = TE_CD_CLI.Text
        frmClientMAJ.ShowDialog()

    End Sub

    Private Sub BO_ENREGISTER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTER.Click

        If IsUserAuthorised(ctGestion) = False Then
            Exit Sub
        End If

        ScreenToDossier()
        Me.Close()

    End Sub

    Private Sub ScreenToDossier()

        If TE_CD_CLI.Text.Length = 0 Then
            MsgBox("Veuillez sélectionner un client !", MsgBoxStyle.Critical)
            Exit Sub
        End If

        If pRubrique = "DEM_DEVIS" Then
            ' maj la demande de devis
            szSQL = "CALL spUPD_DEMANDES('" & gSocieteCode & "'," & pNoDossier & "," & BO_ORDRE.Text & "," & BO_DEVIS.Text & ",'" & _
                 Format(TE_DATE.Value, "yyyy/MM/dd") & "'," & nz(TE_CD_CLI.Text) & ",'" & nq(TE_NOM.Text) & "','" & TE_TYPECLI.Text & "','" & _
                 nq(TE_C_NOM.Text) & "','" & nq(TE_C_ADR.Text) & "','" & nq(TE_C_ADR2.Text) & "','" & TE_C_CP.Text & "','" & nq(TE_C_VILLE.Text) & "','" & _
                 TE_C_TEL1.Text & "','" & TE_C_TEL2.Text & "','" & TE_C_TELP.Text & "','" & TE_C_FAX.Text & "','" & nq(TE_C_OBS.Text) & "','" & _
                 TE_C_ETAGE.Text & "','" & TE_C_CODE.Text & "','" & TE_C_EMAIL.Text & "','" & CB_QUALITE.SelectedItem & "'," & getTailCodeFromCombo(CB_CABINETS.SelectedItem) & "," & _
                 getTailCodeFromCombo(CB_EXPERTS.SelectedItem) & "," & getCodeFromCombo(CB_TVA.SelectedItem) & ",0,'" & nv(TE_NOSIN.Text) & "','" & _
                 nv(TE_NOPOL.Text) & "','" & nv(TE_VREF.Text) & "','" & nv(TE_REF_EXPERT.Text) & "','" & nv(TE_LIBCOMPL.Text) & "','" & Mid(CB_TYPE_TRAV.SelectedItem, 1, 1) & "','" & _
                 TE_GROUPE.Text & "'," & CInt(ChkA_REVOIR.Checked) & "," & CInt(TE_TARIF.Text) & ",'" & TE_TX_TRAV.Text & "'," & TE_MTTRAV.Value & ",'" & _
                 TE_COM_CL.Text & "','" & UCase(gUserCode) & "')"
        Else
            ' maj le rendez-vous
            szSQL = "CALL spUPD_RDVOUS('" & gSocieteCode & "'," & pNoDossier & "," & BO_ORDRE.Text & "," & BO_DEVIS.Text & ",'" & _
                 Format(TE_DATE.Value, "yyyy/MM/dd") & "'," & nz(TE_CD_CLI.Text) & ",'" & nq(TE_NOM.Text) & "','" & TE_TYPECLI.Text & "','" & _
                 nq(TE_C_NOM.Text) & "','" & nq(TE_C_ADR.Text) & "','" & nq(TE_C_ADR2.Text) & "','" & TE_C_CP.Text & "','" & nq(TE_C_VILLE.Text) & "','" & _
                 TE_C_TEL1.Text & "','" & TE_C_TEL2.Text & "','" & TE_C_TELP.Text & "','" & TE_C_FAX.Text & "','" & nq(TE_C_OBS.Text) & "','" & _
                 TE_C_ETAGE.Text & "','" & TE_C_CODE.Text & "','" & TE_C_EMAIL.Text & "','" & CB_QUALITE.SelectedItem & "'," & getTailCodeFromCombo(CB_CABINETS.SelectedItem) & "," & _
                 getTailCodeFromCombo(CB_EXPERTS.SelectedItem) & ",'" & nv(TE_NOSIN.Text) & "','" & _
                 nv(TE_NOPOL.Text) & "','" & nv(TE_VREF.Text) & "','" & nv(TE_REF_EXPERT.Text) & "','" & nv(TE_LIBCOMPL.Text) & "','" & Mid(CB_TYPE_TRAV.SelectedItem, 1, 1) & "','" & _
                 TE_GROUPE.Text & "'," & CInt(ChkA_REVOIR.Checked) & ",'" & TE_TX_TRAV.Text & "'," & TE_MTTRAV.Value & ",'" & _
              TE_COM_CL.Text & "','" & UCase(gUserCode) & "')"
        End If
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount > 0 Then
            pNoDossier = rs.Fields("newnumero").Value
        End If
        rs.Close()

    End Sub

    Private Sub DossierToScreen()

        ' Nouvelle Création ?
        If pNoDossier = 0 Then
            ' RAB des champs du form
            ClearTextBoxes(Me)
            xOldNom = ""      ' ancien nom client

            BO_DEVIS.Text = "0"
            BO_ORDRE.Text = "0"
            TE_DATE.Value = Today

            CB_CABINETS.SelectedIndex = getTailCodePosition(CB_CABINETS, "1")
            xOldCabinet = 1
            ' remplir le combo experts selon le cabinet lu
            InitComboExperts(CB_EXPERTS, "1")
            CB_EXPERTS.SelectedIndex = getTailCodePosition(CB_EXPERTS, "1")
            CB_TVA.SelectedIndex = CB_TVA.FindString("4")
            CB_QUALITE.SelectedIndex = CB_QUALITE.FindString("MR")
            CB_TYPE_TRAV.SelectedIndex = CB_TYPE_TRAV.FindString("S")

            TE_MTTRAV.Value = 0
            TE_NOM.Text = ""
            TE_CD_CLI.Text = ""
            ChkA_REVOIR.Checked = False
            ChkHumidite.Checked = False
            ChkA_REVOIR.ForeColor = Color.Black
            ChkHumidite.ForeColor = Color.Red
        Else
            'Lire le DOSSIER

            szSQL = "CALL spGET_ENTETE('" & gSocieteCode & "','" & pRubrique & "'," & pNoDossier & ")"
            GetRecordset(ctDSN_DEVFAC, szSQL, rs)
            If rs.RecordCount < 1 Then
                rs.Close()
                Exit Sub
            End If

            ' Afficher Entête document
            EntetetoScreen()
            rs.Close()

        End If

    End Sub

    Private Sub EntetetoScreen()

        Dim rsClient As ADODB.Recordset = Nothing

        ' ===== Lire le CLIENT et afficher le bloc CLIENT
        TE_CD_CLI.Text = Format(rs.Fields("cd_cli").Value, "000000")

        szSQL = "CALL spGET_CLIENTS('" & gSocieteCode & "'," & TE_CD_CLI.Text & ")"
        GetRecordset(ctDSN_DEVFAC, szSQL, rsClient)
        If rsClient.RecordCount > 0 Then
            TE_QUALITE.Text = nv(rsClient.Fields("qualite").Value)
            TE_NOM.Text = rsClient.Fields("nom").Value
            xOldNom = TE_NOM.Text     ' ancien nom client
            TE_ADR.Text = fAdresseClient(rsClient)
            TE_COMMENT.Text = nv(rsClient.Fields("comment").Value)
            TE_EMAIL.Text = nv(rsClient.Fields("EMAIL").Value)
            TE_TYPECLI.Text = nv(rsClient.Fields("Type").Value)
            TE_WEBSITE.Text = nv(rsClient.Fields("website").Value)
            TE_NOCOMPTE.Text = nv(rsClient.Fields("NOCOMPTE").Value)
            TE_TARIF.Text = rsClient.Fields("notarif").Value
            TE_GROUPE.Text = rs.Fields("groupe").Value
            ' si le no agrement est vide, on prend celui du client
            'If pRubrique = "DEVIS" Then
            '    If Len(TE_NO_AGREMENT).text = 0 Then
            '        TE_NO_AGREMENT.text = nv(rsClient.Fields("AGREMENT").Value)
            '    End If
            'End If

            'If pRubrique = "DEVIS" Then
            '    TE_NO_AGREMENT.text = nv(rs.Fields("no_agrement").Value)
            'End If
        End If

        ' ===== afficher le bloc : CHANTIER
        CB_QUALITE.SelectedIndex = CB_QUALITE.FindString(rs.Fields("C_QUALITE").Value)
        TE_C_NOM.Text = nv(rs.Fields("c_nom").Value)
        ' si nom chantier non rempli mettre le nom client
        If TE_C_NOM.Text = "" Then
            TE_C_NOM.Text = TE_NOM.Text
        End If
        TE_C_ADR.Text = nv(rs.Fields("c_adr").Value)
        TE_C_ADR2.Text = nv(rs.Fields("C_ADR2").Value)
        TE_C_CP.Text = nv(rs.Fields("C_CP").Value)
        TE_C_VILLE.Text = nv(rs.Fields("C_VILLE").Value)
        TE_C_TEL1.Text = nv(rs.Fields("c_tel1").Value)
        TE_C_TEL2.Text = nv(rs.Fields("c_tel2").Value)
        TE_C_TELP.Text = nv(rs.Fields("c_TELP").Value)
        TE_C_FAX.Text = nv(rs.Fields("C_FAX").Value)
        TE_C_ETAGE.Text = nv(rs.Fields("C_ETAGE").Value)
        TE_C_CODE.Text = nv(rs.Fields("c_code").Value)
        TE_C_OBS.Text = nv(rs.Fields("C_OBS").Value)
        TE_C_EMAIL.Text = nv(rs.Fields("c_EMAIL").Value)
        CB_TYPE_TRAV.SelectedIndex = CB_TYPE_TRAV.FindString(rs.Fields("typeTrav").Value)
        TE_TX_TRAV.Text = nv(rs.Fields("TX_TRAV").Value)
        TE_MTTRAV.Text = nv(rs.Fields("MTTRAV").Value)
        If pRubrique <> "RDVOUS" Then
            CB_TVA.SelectedIndex = CB_TVA.FindString(rs.Fields("codetva").Value)
        End If

        ' ===== afficher le bloc : ASSURANCE
        CB_CABINETS.SelectedIndex = getTailCodePosition(CB_CABINETS, rs.Fields("cabinet").Value)
        xOldCabinet = rs.Fields("cabinet").Value
        ' remplir le combo experts selon le cabinet lu
        InitComboExperts(CB_EXPERTS, getTailCodeFromCombo(CB_CABINETS.SelectedItem))
        CB_EXPERTS.SelectedIndex = getTailCodePosition(CB_EXPERTS, rs.Fields("expert").Value.ToString)
        TE_NOSIN.Text = nv(rs.Fields("noSin").Value)
        TE_NOPOL.Text = nv(rs.Fields("noPol").Value)
        TE_VREF.Text = nv(rs.Fields("VREF").Value)
        TE_REF_EXPERT.Text = nv(rs.Fields("REF_EXPERT").Value)
        TE_LIBCOMPL.Text = nv(rs.Fields("LIBCOMPL").Value)

        ' ===== afficher le bloc : DOSSIER
        ChkA_REVOIR.Checked = rs.Fields("A_REVOIR").Value
        TE_DATE.Value = rs.Fields("Date").Value
        If IsDate(rs.Fields("datecl").Value) Then TE_DATECL.Text = IfDate(rs.Fields("datecl").Value)
        If IsDate(rs.Fields("daterel").Value) Then TE_DATEREL.Text = IfDate(rs.Fields("daterel").Value)
        If IsDate(rs.Fields("Datevi").Value) Then
            TE_DATEVI.Text = IfDate(rs.Fields("Datevi").Value) & " à " & Format(rs.Fields("Heurevi").Value, "HH:mm")
        End If
        TE_COM_CL.Text = nv(rs.Fields("Com_Cl").Value)
        If pRubrique = "ORDRES" Then
            If nv(rs.Fields("humidite").Value) = "OUI" Then
                ChkHumidite.Checked = True
            Else
                ChkHumidite.Checked = False
            End If
        End If
        BO_ORDRE.Text = Format(rs.Fields("NOFACT").Value, "000000")
        BO_DEVIS.Text = Format(rs.Fields("NODEVIS").Value, "000000")
        ' Devis existe ?
        If CLng(BO_DEVIS.Text) > 0 Then
            BO_DEVIS.Enabled = True
        Else
            BO_DEVIS.Enabled = False
        End If
        ' Facture existe
        If CLng(BO_ORDRE.Text) > 0 Then
            BO_ORDRE.Enabled = True
        Else
            BO_ORDRE.Enabled = False
        End If
        TE_DATE_STAMP.Text = "Modifié le " & rs.Fields("DATEMAJ").Value & " par " & UCase(rs.Fields("USERMAJ").Value)

    End Sub

    Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

        Select Case TabControl1.SelectedIndex
            Case 0  ' tab Fiche

            Case 1  ' tab Dossiers
                ' enregistrer le dossier ?
                ScreenToDossier()
                Call InitLignes(0)

            Case 2  ' tab Courriers
                Call InitListeLettres(LV_LETTRES, 0)

        End Select

    End Sub

    Private Sub InitLignes(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_LIGNES.Items.Clear()

        szSQL = "CALL spGET_LIGNES('" & gSocieteCode & "','" & xSousRub & "'," & pNoDossier & ",0)"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Do While Not rs.EOF
            LV_LIGNES.Items.Add(rs.Fields("date").Value)
            If rs.Fields("heurevi").Value.ToString.Length = 0 Then
                LV_LIGNES.Items(nb).SubItems.Add(IfDate(rs.Fields("datevi").Value))
            Else
                LV_LIGNES.Items(nb).SubItems.Add(IfDate(rs.Fields("datevi").Value) & " à " & Format(rs.Fields("heurevi").Value, "HH:mm"))
            End If
            LV_LIGNES.Items(nb).SubItems.Add(nv(rs.Fields("comment").Value))
            LV_LIGNES.Items(nb).SubItems.Add(nv(rs.Fields("commentvi").Value))
            LV_LIGNES.Items(nb).SubItems.Add(nv(rs.Fields("ref_let").Value))
            LV_LIGNES.Items(nb).SubItems.Add(nv(rs.Fields("liste").Value))
            LV_LIGNES.Items(nb).SubItems.Add(IfDate(rs.Fields("datemaj").Value) & " par " & rs.Fields("usermaj").Value)
            LV_LIGNES.Items(nb).SubItems.Add(rs.Fields("nolig").Value)
            If rs.Fields("comment").Value.ToString.Length > 2 Then
                If rs.Fields("comment").Value.ToString.Substring(0, 2) = "!!" Then
                    LV_LIGNES.Items(nb).ForeColor = Color.Red
                End If
            End If

            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_LIGNES)

    End Sub

    Private Sub LV_LETTRES_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_LETTRES.DoubleClick

        If LV_LETTRES.SelectedItems.Count > 0 Then
            ' Créer la lettre dans fichier temporaire
            Call CreerLettre(pRubrique, pNoDossier, LV_LETTRES.SelectedItems(0).Text, IfDate(Now))
            ' puis éditer la lettre selon la société
            'ouverture de l'état .rpt
            Select Case gSocieteCode
                Case "PO"
                    frmViewReport.pNomEtat = "LettreType-PO.rpt"
                Case "PL"
                    frmViewReport.pNomEtat = "LettreType-PL.rpt"
                Case Else
                    frmViewReport.pNomEtat = "LettreType.rpt"
            End Select
            frmViewReport.pParamRPT = ""
            frmViewReport.ShowDialog()
            ' enregistre l'envoi du courrier
            szSQL = "INSERT INTO " & xSousRub & _
                  " (societe,NO_ID, DATE, REF_LET, COMMENT, DATEMAJ, USERMAJ) VALUES('" & _
                  gSocieteCode & "'," & pNoDossier & ",'" & Format(Now, "yyyy-MM-dd") & "','" & LV_LETTRES.SelectedItems(0).Text & "', '" & _
                  nq(LV_LETTRES.SelectedItems(0).SubItems(1).Text) & "','" & Format(Now, "yyyy-MM-dd") & "','" & gUserCode & "');"
            ExecQuery(ctDSN_DEVFAC, szSQL)
        End If

    End Sub

    Private Sub LV_LETTRES_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_LETTRES.KeyDown

        ' touche Enter = DoubleClick
        If e.KeyCode = Keys.Enter Then
            LV_LETTRES_DoubleClick(sender, e)
        End If

    End Sub

    Private Sub BO_REEDITER_Click(sender As System.Object, e As System.EventArgs) Handles BO_REEDITER.Click

        If LV_LIGNES.SelectedItems.Count > 0 Then
            szSQL = LV_LIGNES.SelectedItems(0).SubItems(4).Text
            If szSQL = "" Then
                MsgBox("Sélectionner d'abord une ligne avec une Réf. Courrier", vbInformation)
                Exit Sub
            End If
            'Date courrier
            gDate_debut = LV_LIGNES.SelectedItems(0).SubItems(0).Text

            ' Créer la lettre dans fichier temporaire
            Call CreerLettre(pRubrique, pNoDossier, szSQL, gDate_debut)
            ' puis éditer la lettre selon la société
            'ouverture de l'état .rpt
            Select Case gSocieteCode
                Case "PO"
                    frmViewReport.pNomEtat = "LettreType-PO.rpt"
                Case "PL"
                    frmViewReport.pNomEtat = "LettreType-PL.rpt"
                Case Else
                    frmViewReport.pNomEtat = "LettreType.rpt"
            End Select
            frmViewReport.pParamRPT = ""
            frmViewReport.ShowDialog()
        End If

    End Sub

    Private Sub LV_LIGNES_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_LIGNES.DoubleClick

        If IsUserAuthorised(ctGestion) = False Then
            Exit Sub
        End If

        If LV_LIGNES.SelectedItems.Count > 0 Then
            xNolig = LV_LIGNES.SelectedItems(0).SubItems(7).Text
            Call LignetoScreen(xNolig)
            ' afficher les détails de la formule
            GB_DETAIL.Visible = True
            TE_lg_DATE.Focus()
        End If

    End Sub

    Private Sub LV_LIGNES_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_LIGNES.KeyDown

        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                Call LV_LIGNES_DoubleClick(sender, e)

            Case Keys.Insert
                xNolig = 0
                ClearTextBoxes(GB_DETAIL)
                TE_lg_DATE.Value = Now
                CB_AGENDA.SelectedIndex = CB_AGENDA.FindString("X")
                GB_DETAIL.Visible = True
                TE_lg_DATE.Focus()

            Case Keys.Delete
                If LV_LIGNES.SelectedItems.Count > 0 Then
                    xNolig = LV_LIGNES.SelectedItems(0).SubItems(7).Text
                    Call LignetoScreen(xNolig)
                    deleteEnregistrement(xSousRub, LV_LIGNES.SelectedItems(0).SubItems(2).Text, pNoDossier, xNolig)
                    Call InitLignes(xNolig)
                    ' Cacher les détails
                    GB_DETAIL.Visible = False
                End If
        End Select

    End Sub

    Private Sub LignetoScreen(ByVal xNolig As Integer)

        szSQL = "CALL spGET_LIGNES('" & gSocieteCode & "','" & xSousRub & "'," & pNoDossier & "," & xNolig & ")"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount < 1 Then
            rs.Close()
            Exit Sub
        End If

        TE_lg_DATE.Value = rs.Fields("Date").Value
        TE_lg_DATEVI.Text = IfDate(rs.Fields("Datevi").Value)
        TE_lg_HEUREVI.Text = IfHeure(rs.Fields("Heurevi").Value)
        TE_lg_COMMENT.Text = nv(rs.Fields("comment").Value)
        TE_lg_COMMENTVI.Text = nv(rs.Fields("commentvi").Value)
        TE_lg_REF_LET.Text = nv(rs.Fields("REF_LET").Value)
        CB_AGENDA.SelectedIndex = CB_AGENDA.FindString(nv(rs.Fields("Liste").Value))
        rs.Close()
        GB_DETAIL.Visible = True

    End Sub

    Private Sub CB_AGENDA_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles CB_AGENDA.KeyDown

        If e.KeyCode = Keys.Return Then
            Call ScreenToLigne(xNolig)
            GB_DETAIL.Visible = False
            If xNolig = 0 Then
                InitLignes(LV_LIGNES.Items.Count)
            Else
                InitLignes(xNolig)
            End If
            LV_LIGNES.Focus()
        End If

    End Sub

    Private Sub ScreenToLigne(ByVal nNolig As Integer)
        Dim Datevi As String
        Dim Heurevi As String
        Dim szAgenda As String

        ' date de rendez-vous saisie
        If IsDate(TE_lg_DATEVI.Text) = True Then
            Datevi = Format(CDate(TE_lg_DATEVI.Text), "yyyy/MM/dd")
            ' remplacer le point par le double-point
            TE_lg_HEUREVI.Text = TE_lg_HEUREVI.Text.Replace(".", ":")
            If IsDate(TE_lg_HEUREVI.Text) = True Then
                Heurevi = TE_lg_HEUREVI.Text
                szAgenda = CB_AGENDA.SelectedItem.ToString.Substring(0, 1)
                ' controler que la personne n'a pas déjà un RDV ce jour là
                ' rechercher dans toutes les societes
                If szAgenda <> "N" Then
                    szSQL = "CALL spGET_LIGNES_MULTIPLES(" & pNoDossier & ",'" & Datevi & "','" & Heurevi & "','" & CB_AGENDA.SelectedItem.ToString.Substring(0, 1) & "')"
                    GetRecordset(ctDSN_DEVFAC, szSQL, rs)
                    If rs.RecordCount > 0 Then
                        MsgBox("Cette personne a déjà un RDV de type " & rs.Fields("origine").Value & " avec " & rs.Fields("nomcli").Value & " (" & rs.Fields("nomcli").Value & ")", _
                               MsgBoxStyle.Information, "RDV Multiples")
                        Exit Sub
                    End If
                    rs.Close()
                End If
            Else
                Heurevi = ""
            End If
        Else
            Datevi = ""
            Heurevi = ""
        End If

        ' maj une nouvelle ligne
        szSQL = "CALL spUPD_ORDRES_LIGNE(" & nNolig & ",'" & gSocieteCode & "','" & xSousRub & "'," & pNoDossier & ",'" & _
                Format(TE_lg_DATE.Value, "yyyy/MM/dd") & "','" & Datevi & "','" & Heurevi & "','" & nq(TE_lg_COMMENT.Text) & "','" & _
                nq(TE_lg_COMMENTVI.Text) & "','" & CB_AGENDA.SelectedItem.ToString.Substring(0, 1) & "','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)

    End Sub

    Private Sub TE_lg_DATE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_lg_DATE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_lg_DATEVI_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_lg_DATEVI.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_lg_HEUREVI_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_lg_HEUREVI.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_lg_COMMENT_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_lg_COMMENT.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_lg_COMMENTVI_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_lg_COMMENTVI.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub BO_IMPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_IMPRIMER.Click
        gNoDossier = pNoDossier
        frmEditions.pRubrique = pRubrique
        frmEditions.ShowDialog()
    End Sub

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        If IsUserAuthorised(ctCompta) = False Then
            Exit Sub
        End If

        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If

        deleteEnregistrement(pRubrique, pRubrique & " de " & TE_NOM.Text, pNoDossier)
        ' ferme la fenetre
        Me.Close()

    End Sub

    Private Sub TE_ADR_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_ADR.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_COM_CL_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_COM_CL.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_COMMENT_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_COMMENT.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_DATE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_DATE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_EMAIL_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_EMAIL.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_LIBCOMPL_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_LIBCOMPL.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_MTTRAV_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_MTTRAV.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_NOPOL_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOPOL.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_NOSIN_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOSIN.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_QUALITE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_QUALITE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_REF_EXPERT_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_REF_EXPERT.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_TX_TRAV_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_TX_TRAV.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_VREF_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_VREF.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub CB_CABINETS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles CB_CABINETS.KeyDown

        If e.KeyCode = Keys.Enter Then
            ' code cabinet a changé ?
            If CB_CABINETS.SelectedIndex <> xOldCabinet Then
                ' remplir le combo experts selon le cabinet lu
                InitComboExperts(CB_EXPERTS, getTailCodeFromCombo(CB_CABINETS.SelectedItem))
            End If
            Call Perform_tab_on_enter(e)
        End If

    End Sub

    Private Sub CB_EXPERTS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles CB_EXPERTS.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub CB_QUALITE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles CB_QUALITE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub CB_TVA_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles CB_TVA.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub CB_TYPE_TRAV_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles CB_TYPE_TRAV.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_ADR_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_ADR.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_ADR2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_ADR2.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_CODE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_CODE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_CP_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_CP.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_EMAIL_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_EMAIL.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_ETAGE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_ETAGE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_FAX_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_FAX.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_NOM_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_NOM.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_OBS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_OBS.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_TEL1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_TEL1.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_TEL2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_TEL2.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_TELP_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_TELP.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_C_VILLE_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_C_VILLE.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub ChkA_REVOIR_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles ChkA_REVOIR.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub ChkHumidite_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles ChkHumidite.KeyDown
        Call Perform_tab_on_enter(e)
    End Sub

    Private Sub TE_NOM_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown

        If e.KeyCode = Keys.Return Then
            If TE_NOM.Text.Length = 0 Then
                MsgBox("Veuillez saisir un nom de client.", MsgBoxStyle.Critical)
                Exit Sub
            End If
            If TE_NOM.Text <> xOldNom Then
                ' le nom a changé, effectue la recherche
                frmClientSearch.pNOM_CLI = TE_NOM.Text
                frmClientSearch.pCD_CLI = 0
                frmClientSearch.ShowDialog()

                If frmClientSearch.pCD_CLI > 0 Then
                    TE_CD_CLI.Text = frmClientSearch.pCD_CLI
                    'Lire la Table  CLIENT par No du Client
                    szSQL = "CALL spGET_CLIENTS('" & gSocieteCode & "'," & TE_CD_CLI.Text & ")"
                    GetRecordset(ctDSN_DEVFAC, szSQL, rs)
                    If rs.RecordCount > 0 Then
                        TE_QUALITE.Text = nv(rs.Fields("qualite").Value)
                        TE_NOM.Text = rs.Fields("nom").Value
                        TE_ADR.Text = fAdresseClient(rs)
                        If TE_C_NOM.Text.Length = 0 Then
                            TE_C_NOM.Text = TE_NOM.Text
                            TE_C_ADR.Text = nv(rs.Fields("adresse").Value)
                            TE_C_ADR2.Text = nv(rs.Fields("adresse2").Value)
                            TE_C_CP.Text = nv(rs.Fields("cp").Value)
                            TE_C_VILLE.Text = nv(rs.Fields("ville").Value)
                        End If
                        TE_TYPECLI.Text = nv(rs.Fields("type").Value)
                        TE_GROUPE.Text = nv(rs.Fields("groupe").Value)
                        TE_NOCOMPTE.Text = nv(rs.Fields("nocompte").Value)
                        TE_TARIF.Text = nv(rs.Fields("notarif").Value)
                        TE_WEBSITE.Text = nv(rs.Fields("website").Value)
                        TE_COMMENT.Text = nv(rs.Fields("comment").Value)
                        If pNoDossier = 0 Then
                            ' si création, remplir le code cabinet selon le client
                            CB_CABINETS.SelectedIndex = getTailCodePosition(CB_CABINETS, rs.Fields("cabinet").Value)
                            xOldCabinet = rs.Fields("cabinet").Value
                            ' remplir le combo experts selon le cabinet lu
                            InitComboExperts(CB_EXPERTS, getTailCodeFromCombo(CB_CABINETS.SelectedItem))
                            CB_EXPERTS.SelectedIndex = getTailCodePosition(CB_EXPERTS, rs.Fields("expert").Value.ToString)
                        End If

                    End If
                    rs.Close()
                End If
            End If
            ' passe au champs suivant
            Call Perform_tab_on_enter(e)
        End If
    End Sub

    Private Sub BO_DEVIS_Click(sender As System.Object, e As System.EventArgs) Handles BO_DEVIS.Click
        ' affiche les détails du dossier sélectionné
        Call Show_Dossier("DE", BO_DEVIS.Text)
    End Sub

    Private Sub BO_ORDRE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ORDRE.Click
        ' affiche les détails du dossier sélectionné
        Call Show_Dossier("OS", BO_ORDRE.Text)
    End Sub

    Private Sub BO_CONVERTIR_Click(sender As System.Object, e As System.EventArgs) Handles BO_CONVERTIR.Click
        Dim xVersRub As String

        If RB_CREER_OS.Checked Then
            ' convertir cette DD en OS
            xVersRub = "OS"
            szSQL = "CALL spINS_ORDRES_FROM_DOSSIER('" & gSocieteCode & "','DD'," & pNoDossier & ",'" & gUserCode & "')"
            GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Else
            ' convertir cette DD en DE
            xVersRub = "DE"
            szSQL = "CALL spINS_DEVIS_FROM_DOSSIER('" & gSocieteCode & "','DD'," & pNoDossier & ",0,'" & gUserCode & "')"
            GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        End If

        If rs.RecordCount > 0 Then
            'ouvrir le nouveau dossier créé
            Call Show_Dossier(xVersRub, rs.Fields("last_insert_id").Value)
            Me.Close()
        End If

    End Sub

    Private Sub BO_CABINET_Click(sender As System.Object, e As System.EventArgs) Handles BO_CABINET.Click
        ' afficher le Cabinet 
        frmCabinetMAJ.pCode_cab = getTailCodeFromCombo(CB_CABINETS.SelectedItem)
        frmCabinetMAJ.ShowDialog()
    End Sub


End Class